package com.tianyang.yukuaifu.base;

import android.content.Context;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.hawk.NoEncryption;
import com.tencent.bugly.Bugly;
import com.tencent.smtt.sdk.QbSdk;
import com.tianyang.yukuaifu.BuildConfig;
import com.tianyang.yukuaifu.jump.jsbridge.JsBridgeConfig;
import com.tianyang.yukuaifu.jump.jsbridge.ServiceModule;
import com.tianyang.yukuaifu.jump.shareutil.ShareConfig;
import com.tianyang.yukuaifu.jump.shareutil.ShareManager;
import com.tianyang.yukuaifu.utils.AppContextUtil;
import com.tianyang.yukuaifu.utils.DeviceUtil;
import com.tianyang.yukuaifu.utils.TimeManager;

import cn.jpush.android.api.JPushInterface;

/**
 * Description：应用入口
 */

public class YkfApplication extends MultiDexApplication {

    private static YkfApplication instance;

    private boolean isInitModule = false;

    public static YkfApplication getInstance() {
        if (null == instance) {
            instance = new YkfApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        AppContextUtil.initApp(this);
        DeviceUtil.init(this);
        Hawk.init(this).setEncryption(new NoEncryption()).build();
        Fresco.initialize(this);
    }

    public void initModule() {
        if (isInitModule) return;
        isInitModule = true;
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
            }

            @Override
            public void onViewInitFinished(boolean b) {
            }
        });
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                TimeManager.getInstance().initLoginTime();
            }
        }).start();

        if (!BuildConfig.DEBUG)
            Bugly.init(this, BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);

        JsBridgeConfig.getSetting().setProtocol("MyBridge").registerDefaultModule(
                ServiceModule.class
        ).debugMode(false);

        ShareConfig config = ShareConfig.instance()
                .wxId(BuildConfig.WX_APP_ID)
//                .wxSecret(CommonConfig.WX_SECRET)
                .debug(false);
        ShareManager.init(config);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}
